﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RadowCore.Models;
namespace RadowCore.Helpers
{
    static class PGHasher
    {
        public static string GetHashValue(PGNode Graph, List<PGNode> allNodes)
        {
            string hashValue = "";
            //sort the allNode list alphabetically on node Activity ID
            //allNodes.Sort();
            var sortedAllNodes = allNodes.OrderBy(n=>n.Name);
            // build the adjency matrix doing 2 loops on the allNode list to check if there is an edge between two nodes if yes get the value else put 0
            string value = "";
            for (int i = 0; i < sortedAllNodes.Count(); i++)
            {
                PGNode sourceNode = sortedAllNodes.ElementAt(i);
                for (int j = 0; j < sortedAllNodes.Count(); j++)
                {   
                    PGNode targetNode = sortedAllNodes.ElementAt(j);
                    value = sourceNode.GetTargetValue(targetNode.Name);
                    if (value != null)
                    {
                        hashValue += sourceNode.Name + ":" + sourceNode.Orignator + "->" + targetNode.Name + ":" + targetNode.Orignator + ":" + value;
                    }
                    else
                    {
                        hashValue += "$$";// a special value represents a no edge case
                    }
                }
            }

            //get the hashcode for the adjency matrix 
            return hashValue;
        }
    }
}
